package cn.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cn.entity.Teacher;
import cn.service.TeacherService;
import core.utils.ConvertJSONUtil;
import core.utils.MD5Util;
import core.utils.StringUtil;

@Controller(value="teacherController")
@RequestMapping(value="/teachers")
public class TeacherController {
	
	private  TeacherService teacherService;
	@Autowired
	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}
	// select Teacher list 
	@RequestMapping(value="list")
	public String findall(HttpServletRequest request){
		String tname=request.getParameter("tname");
		String status=request.getParameter("status");
		String createdate=request.getParameter("createdate");
		String teid=request.getParameter("teid");
		Map map=new HashMap<>();
		if(StringUtil.isNotEmpty(tname)){
			map.put("tname", "%" + tname + "%");
			request.setAttribute("tname", tname);
		}
		if(StringUtil.isNotEmpty(status)){
			System.out.println(status+"status-----");
			map.put("status", status);
			request.setAttribute("status", status);
		}
		if(StringUtil.isNotEmpty(createdate)){
			map.put("createdate", createdate);
			request.setAttribute("createdate", createdate);
		}
		if(StringUtil.isNotEmpty(teid)){
			map.put("teid", teid);
			request.setAttribute("teid", teid);
		}
		List<Teacher> list=teacherService.findby(map);
		request.setAttribute("list", list);
		
		return "list";
	}
	@RequestMapping(value="login")
	public ModelAndView login(HttpSession session,HttpServletRequest request){
		Map map=new HashMap<>();
		String tname=request.getParameter("tname");
		String password=request.getParameter("password");
		String pass=MD5Util.getMD5(password.getBytes());
		map.put("tname", tname);
		map.put("pwssword", pass);
		ModelAndView mv=new ModelAndView();
		Teacher teacher=teacherService.login(map);
		if(teacher!=null) {
			session.setAttribute("teacher", teacher);
			mv.setViewName("teacherlist");
		}else {
			mv.addObject("shibai","1");
			mv.setViewName("login");
		}
		return mv;
	}
		//get one
		@RequestMapping(value="get")
		public String get(HttpServletRequest request){
			Map map=new HashMap<>();
			String tname=request.getParameter("tname");
			String tid=request.getParameter("tid");
			map.put("tname", tname);
			map.put("tid", tid);
			Teacher teacher=teacherService.get(map);
			request.setAttribute("teacher", teacher);
			return "list";
		}
		//add 
		@RequestMapping(value="add")
		public String add(Teacher teacher){
			teacher.setCreatedate(new Date());
			teacher.setStatus("1");
			changevalues(teacher);
			
			//md5加密
			teacher.setPassword(MD5Util.getMD5(teacher.getPassword().getBytes()));
			teacherService.add(teacher);
			
			addmiddleclass(teacher.getTname());
			
			return "teacherlist";
		}
		//添加中间表
		private void addmiddleclass(String tname) {
			// TODO Auto-generated method stub
			Teacher t2=teacherService.findone(tname);
			String [] arr= t2.getSubjectid().split(",");
			Map m1=new  HashMap<>();
			m1.put("tid",t2.getTid());
			for (int i = 0; i < arr.length; i++) {
				m1.remove("sid");
				m1.put("sid",arr[i]);
				teacherService.addmiddlesubject(m1);
			}
			String [] arr2= t2.getClassid().split(",");
			for (int i = 0; i < arr2.length; i++) {
				m1.remove("cid");
				m1.put("cid",arr2[i]);
				teacherService.addmiddleclass(m1);
			}
		}
		//delete
		@RequestMapping(value="delete")
		public String delete(int tid){
			teacherService.delete(tid);
			return "teacherlist";
		}
		//update 
		@RequestMapping(value="update")
		public String update(Teacher teacher){
			changevalues(teacher);
			teacherService.update(teacher);
			return "teacherlist";
		}
		@RequestMapping(value="doupdate")
		public String doupdate(HttpServletRequest request){
			Map map=new HashMap<>();
			String tid=request.getParameter("tid");
			map.put("tid", tid);
			Teacher teacher=teacherService.get(map);
			request.setAttribute("teacher", teacher);
			return "update";
		}
		/**
		 * 老师是否存在
		 * 
		 * @param name
		 * @param response
		 */
		@RequestMapping("checkName")
		public void checkName(String tname, HttpServletResponse response) {
			Integer count = teacherService.checkName(tname);
			if (count > 0) {
				ConvertJSONUtil.toText("1", response);
			} else {
				ConvertJSONUtil.toText("0", response);
			}
		}
	public void changevalues(Teacher teacher) {
		if(teacher.getSubjectid().equals("语文")) {
			teacher.setSubjectid("1");
		}else if(teacher.getSubjectid().equals("数学")) {
			teacher.setSubjectid("2");
		}else if(teacher.getSubjectid().equals("英语")) {
			teacher.setSubjectid("3");
		}else if(teacher.getSubjectid().equals("化学")) {
			teacher.setSubjectid("4");
		}else if(teacher.getSubjectid().equals("物理")) {
			teacher.setSubjectid("5");
		}else if(teacher.getSubjectid().equals("体育")) {
			teacher.setSubjectid("6");
		}else if(teacher.getSubjectid().equals("历史")) {
			teacher.setSubjectid("7");
		}else if(teacher.getSubjectid().equals("地理")) {
			teacher.setSubjectid("8");
		}else if(teacher.getSubjectid().equals("生物")) {
			teacher.setSubjectid("9");
		}else if(teacher.getSubjectid().equals("音乐")) {
			teacher.setSubjectid("10");
		}else if(teacher.getSubjectid().contains(",")) {
			
		}
		else {
			//teacher.setSubjectid("1");
		}
		
		
		if(teacher.getRoleid().equals("校长")) {
			teacher.setRoleid("1");
			
		}else if(teacher.getRoleid().equals("主任")) {
			teacher.setRoleid("2");
			
		}else if(teacher.getRoleid().equals("老师")) {
			teacher.setRoleid("3");
		}else {
		}
	}
}
